package com.longdaji.decoration.ui.activitiesOfMine.accountManagement.changePhone;

import android.text.TextUtils;
import android.util.Log;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.LogUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/1/23 0023.
 */

public class ChangePhonePresenter extends RxPresenter<ChangePhoneContract.View> implements ChangePhoneContract.Presenter {
    private DataManager mDataManager;

    @Inject
    public ChangePhonePresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void onGetCodeClick(String newTelephone) {
        addSubscribe(mDataManager.doVerificationRequest(newTelephone, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d("mytest", "sendVerificationRequest success");
                        mView.showSendVerification();
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("mytest", "msg : " + msg);
                    }
                }));
    }

    @Override
    public void onConfirmClick(final String newTelephone, String code) {
        addSubscribe(mDataManager.doVerificationResult(newTelephone, code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d("mytest", "sendVerificationResult success");
                        doUpdateTelephone(newTelephone);
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("mytest", "msg : " + msg);
                        mView.showCodeError();
                    }
                }));
    }

    private void doUpdateTelephone(String newTelephone) {
        addSubscribe(mDataManager.doUpdateAccountApi(Account.getInstance().getUserid(), null, newTelephone, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d("succeed : doUpdateAccountApi ");
                        mView.showResult(true);

                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);
                        mView.showResult(false);
                    }
                }));
    }
}
